package com.aft.terra.modules.game.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.aft.terra.app.Constants;
import com.aft.terra.common.utils.DateUtil;
import com.aft.terra.common.utils.RSASig;
import com.aft.terra.common.utils.RedisKit;
import com.aft.terra.modules.game.model.GameDetails;
import com.aft.terra.modules.game.model.Orders;
import com.aft.terra.modules.game.model.User;
import com.aft.terra.modules.game.service.PaymentService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

public class PaymentServiceImpl implements PaymentService{

	/**
	 * @author Mr.Ni
	 * 建行支付回调
	 */
	/**
	 * 建行支付回调
	 */
	public void callBack(Controller controller) {
		// String posId = controller.getPara("POSID", ""); // POSID 商户柜台代码
		// CHAR(9)从商户传送的信息中获得
		// String branchId = controller.getPara("BRANCHID", ""); // BRANCHID
		// 分行代码 CHAR(9)从商户传送的信息中获得
		// String orderCode = controller.getPara("ORDERID", ""); // ORDERID 定单号
		// CHAR(30)从商户传送的信息中获得
		// String payment = controller.getPara("PAYMENT", ""); // PAYMENT 付款金额
		// NUMBER(16,2)从商户传送的信息中获得
		// String curCode = controller.getPara("CURCODE", ""); // CURCODE 币种
		// CHAR(2) 从商户传送的信息中获得
		String remark1 = controller.getPara("REMARK1", "");
		// String remark2 = controller.getPara("REMARK2", "");
		// String acc_type = controller.getPara("ACC_TYPE", "");
		// String success = controller.getPara("SUCCESS", ""); // SUCCESS 成功标志
		// CHAR(1)成功时返回Y N
		// String sign = controller.getPara("SIGN", ""); // SIGN 数字签名 CHAR(256)\
		// String type = controller.getPara("TYPE", "");
		// String referer = controller.getPara("REFERER", "");
		// String clientip = controller.getPara("CLIENTIP", "");
		//
		Map<String, String> map = new HashMap<>();
		map.put("POSID", controller.getPara("POSID", "")); // POSID 商户柜台代码
															// CHAR(9)从商户传送的信息中获得
		map.put("BRANCHID", controller.getPara("BRANCHID", "")); // BRANCHID
																	// 分行代码
																	// CHAR(9)从商户传送的信息中获得
		map.put("ORDERID", controller.getPara("ORDERID", "")); // ORDERID 定单号
																// CHAR(30)从商户传送的信息中获得
		map.put("PAYMENT", controller.getPara("PAYMENT", "")); // PAYMENT 付款金额
																// NUMBER(16,2)从商户传送的信息中获得
		map.put("CURCODE", controller.getPara("CURCODE", "")); // CURCODE 币种
																// CHAR(2)
																// 从商户传送的信息中获得
		map.put("REMARK1", controller.getPara("REMARK1", ""));
		map.put("REMARK2", controller.getPara("REMARK2", ""));
		map.put("ACC_TYPE", controller.getPara("ACC_TYPE", ""));
		map.put("SUCCESS", controller.getPara("SUCCESS", "")); // SUCCESS 成功标志
																// CHAR(1)成功时返回Y
																// N
		map.put("SIGN", controller.getPara("SIGN", "")); // SIGN 数字签名 CHAR(256)\
		map.put("TYPE", controller.getPara("TYPE", ""));
		map.put("REFERER", controller.getPara("REFERER", ""));
		map.put("CLIENTIP", controller.getPara("CLIENTIP", ""));
		String result = HttpKit.post(remark1, map, JsonKit.toJson(map));
		LogKit.info("======================== result " + result + "==========================");
	}

	@Before(Tx.class)
	public String usedCallBack(Controller controller) {
		Boolean paySuccess = Boolean.FALSE;
		String posId = controller.getPara("POSID", ""); // POSID 商户柜台代码
														// CHAR(9)从商户传送的信息中获得
		String branchId = controller.getPara("BRANCHID", ""); // BRANCHID 分行代码
																// CHAR(9)从商户传送的信息中获得
		String orderCode = controller.getPara("ORDERID", ""); // ORDERID 定单号
																// CHAR(30)从商户传送的信息中获得
		String payment = controller.getPara("PAYMENT", ""); // PAYMENT 付款金额
															// NUMBER(16,2)从商户传送的信息中获得
		String curCode = controller.getPara("CURCODE", ""); // CURCODE 币种
															// CHAR(2)
															// 从商户传送的信息中获得
		String remark1 = controller.getPara("REMARK1", "");
		String remark2 = controller.getPara("REMARK2", "");
		String accType = controller.getPara("ACC_TYPE", "");
		String success = controller.getPara("SUCCESS", ""); // SUCCESS 成功标志
															// CHAR(1)成功时返回Y N
		String sign = controller.getPara("SIGN", ""); // SIGN 数字签名 CHAR(256)
		StringBuffer src = new StringBuffer();
		src.append("POSID=").append(posId).append("&BRANCHID=").append(branchId).append("&ORDERID=").append(orderCode)
				.append("&PAYMENT=").append(payment).append("&CURCODE=").append(curCode).append("&REMARK1=")
				.append(remark1).append("&REMARK2=").append(remark2);
		if (StrKit.notBlank(accType)) {
			src.append("&ACC_TYPE=").append(accType);
		}
		src.append("&SUCCESS=").append(success).append("&TYPE=" + controller.getPara("TYPE", ""))
				.append("&REFERER=" + controller.getPara("REFERER", ""))
				.append("&CLIENTIP=" + controller.getPara("CLIENTIP", ""));
		boolean bRet;
		RSASig rsa = new RSASig();
		rsa.setPublicKey(Constants.CCB_PAY_KEY);
		bRet = rsa.verifySigature(sign.toString().trim(), src.toString().trim());
		Orders order = Orders.dao.findByCode(orderCode);
		LogKit.info("================== pay success " + success + "========================");
		LogKit.info("================== sign " + bRet + "===================================");
		if (bRet) {
			if (success.equals("Y")) {// 支付成功
				String lockKey = RedisKit.getCache().get(orderCode);
				if (order.getPaySuccess() == 0 && StrKit.notBlank(lockKey)
						&& Orders.dao.queryCount(Long.valueOf(remark2), DateUtil.now()) < 30) {
					paySuccess = Orders.dao.setOrderPaySuccess(orderCode);
					User.dao.updateUserGameCount(order.getUserId().toString(),1);
				}
				if (paySuccess) {
					controller.setAttr("message", "支付成功");
				} else {
					controller.setAttr("message", "支付失败");
				}
			} else {// 支付成功失败
				controller.setAttr("message", "支付失败");
			}
		}
		RedisKit.getCache().del(orderCode);
		return "/";
	}


	public String newCallBack(Controller controller) {
		Boolean paySuccess = Boolean.FALSE;
		String posId = controller.getPara("POSID", ""); // POSID 商户柜台代码  CHAR(9)从商户传送的信息中获得
		String branchId = controller.getPara("BRANCHID", ""); // BRANCHID 分行代码  CHAR(9)从商户传送的信息中获得
		String orderCode = controller.getPara("ORDERID", ""); // ORDERID 定单号 CHAR(30)从商户传送的信息中获得
		String payment = controller.getPara("PAYMENT", ""); // PAYMENT 付款金额  NUMBER(16,2)从商户传送的信息中获得
		String curCode = controller.getPara("CURCODE", ""); // CURCODE 币种 CHAR(2) 从商户传送的信息中获得
		String remark1 = controller.getPara("REMARK1", "");
		String remark2 = controller.getPara("REMARK2", "");
		String accType = controller.getPara("ACC_TYPE", "");
		String success = controller.getPara("SUCCESS", ""); // SUCCESS 成功标志 CHAR(1)成功时返回Y N
		String sign = controller.getPara("SIGN", ""); // SIGN 数字签名 CHAR(256)
		StringBuffer src = new StringBuffer();
		src.append("POSID=").append(posId).append("&BRANCHID=").append(branchId).append("&ORDERID=").append(orderCode)
				.append("&PAYMENT=").append(payment).append("&CURCODE=").append(curCode).append("&REMARK1=")
				.append(remark1).append("&REMARK2=").append(remark2);
		if (StrKit.notBlank(accType)) {
			src.append("&ACC_TYPE=").append(accType);
		}
		src.append("&SUCCESS=").append(success).append("&TYPE=" + controller.getPara("TYPE", ""))
				.append("&REFERER=" + controller.getPara("REFERER", ""))
				.append("&CLIENTIP=" + controller.getPara("CLIENTIP", ""));
		boolean bRet;
		RSASig rsa = new RSASig();
		rsa.setPublicKey(Constants.CCB_PAY_KEY);
		bRet = rsa.verifySigature(sign.toString().trim(), src.toString().trim());
		Orders order = Orders.dao.findByCode(orderCode);
		LogKit.info(" ================= "+ bRet +"   --------------------");
		if (bRet) {
			if (success.equals("Y")) {// 支付成功
				String lockKey = RedisKit.getCache().get(orderCode);
				LogKit.info(" ================= "+ lockKey +"   --------------------");
				if (order.getPaySuccess() == 0 && StrKit.notBlank(lockKey) && 
						Orders.dao.queryCount(Long.valueOf(remark2), DateUtil.now()) < 30) {
					LogKit.info(" ================= "+ 123123123 +"   --------------------");
					Integer userCount = Orders.dao.queryCount(Long.valueOf(remark2),DateUtil.now());
					Integer sCount = 30 -userCount;
					paySuccess = Orders.dao.setOrderPaySuccess(orderCode);
					if(sCount > 5 ){
						order.setGameCount(5);
						User.dao.updateUserGameCount(order.getUserId().toString(),5);
					}else{
						order.setGameCount(sCount);
						User.dao.updateUserGameCount(order.getUserId().toString(),sCount);
					}
					order.update();
				}
				if(paySuccess){
					controller.setAttr("message", "支付成功");
				}else{
					controller.setAttr("message", "支付失败");
				}
			} else {// 支付成功失败
				controller.setAttr("message", "支付失败");
			}
		} 
		RedisKit.getCache().del(orderCode);
		return "/";
	}


}
